package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
//实现converter自定义参数类型转换
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String source) {

        //枚举对象.values()->获取枚举内部的具体对象类型集合
        for (ItemType itemType : ItemType.values()) {
            if (itemType.getCode() == Integer.parseInt(source)){
                return itemType;
            }
        }
        throw new RuntimeException("数据类型错误，值必须是1/2;");
    }
}
